package lucky.xiong.实际工作问题;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class 读取超长文件日志某一行 {


        public static void main(String[] args) {
            String fileName = "D:\\doc\\bug\\工行修改组织机构报错-多岗\\2023-07-06\\iamadmin.20230706.0.log";
            int lineNumber = 10912;
            String jsonStr = "";
            try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
                String line;
                int currentLine = 1;
                while ((line = br.readLine()) != null) {
                    if (currentLine == lineNumber) {
                        String[] parts = line.split("data => ");
                        jsonStr = parts[1];
                        break;
                    }
                    currentLine++;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }



            JSONObject jsonObject = JSONObject.parseObject(jsonStr);
            JSONArray data = jsonObject.getJSONArray("data");
            for (int i = 0; i < data.size(); i++) {
                System.out.println(data.get(i));
            }
            System.out.println("数据长度："+data.size());
            // 根据code属性查找重复的JSON对象
            List<JSONObject> duplicates = findDuplicatesByCode(data);
            // 按照code值排序重复的JSON对象
//            Collections.sort(duplicates, Comparator.comparing(jsonObj -> jsonObj.getString("code")));
            // 输出重复的JSON对象
            System.out.println("重复code的JSON对象:");
            for (JSONObject duplicate : duplicates) {
                System.out.println(duplicate);
            }
        }


    // 根据code属性查找重复的JSON对象
    private static List<JSONObject> findDuplicatesByCode(JSONArray jsonArray) {
        Set<String> uniqueCodes = new HashSet<>();
        List<JSONObject> duplicates = new ArrayList<>();

        for (Object obj : jsonArray) {
            JSONObject jsonObj = (JSONObject) obj;
            String code = jsonObj.getJSONObject("attrs").getString("code");
            if (uniqueCodes.contains(code)) {
                duplicates.add(jsonObj);
            } else {
                uniqueCodes.add(code);
            }
        }

        return duplicates;
    }
}
